package org.example.admin.airway;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;


public class updateAirwayInfoView extends JFrame {
    private JTextField airwayIdField,departureField,arrivalField;
    private JTextField usernameField;
    private JButton updateButton;

    public updateAirwayInfoView() {
        setTitle("管理端航线信息修改页面");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(7, 2));

        JLabel airwayIdLabel = new JLabel("航线id");
        airwayIdField = new JTextField();

        JLabel departureLabel = new JLabel("出发点");
        departureField = new JTextField();

        JLabel arrivalLabel = new JLabel("目的地");
        arrivalField = new JTextField();

        updateButton = new JButton("更新");

        // 添加组件到窗口
        add(airwayIdLabel);
        add(airwayIdField);

        add(departureLabel);
        add(departureField);

        add(arrivalLabel);
        add(arrivalField);

        add(new JLabel()); // 占位
        add(updateButton);

        // 添加更新按钮的点击事件监听器
        updateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                updateUserInfo();
            }
        });
    }

    private void updateUserInfo() {
        int airwayId = Integer.parseInt(airwayIdField.getText());
        String departure=departureField.getText();
        String arrival=arrivalField.getText();

        try {
            // 连接数据库
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:Mysql://localhost:3306/airlines", "root", "123456");

            // 创建更新语句
            String updateQuery = "UPDATE air_airway SET departure=?,arrival=? WHERE airway_id = ?";
            PreparedStatement statement = conn.prepareStatement(updateQuery);


            statement.setString(1, departure);
            statement.setString(2,arrival);
            statement.setInt(3, airwayId);

            // 执行更新操作
            int rowsUpdated = statement.executeUpdate();

            // 关闭连接和语句
            statement.close();
            conn.close();

            if (rowsUpdated > 0) {
                JOptionPane.showMessageDialog(this, "更新成功");
            } else {
                JOptionPane.showMessageDialog(this, "用户不存在或更新失败");
            }

            // 清空输入框
            clearFields();

        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "更新失败：" + e.getMessage());
        }
    }

    private void clearFields() {
        airwayIdField.setText("");
        departureField.setText("");
        arrivalField.setText("");
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            updateAirwayInfoView updateAirwayInfoForm = new updateAirwayInfoView();
            updateAirwayInfoForm.setSize(300, 150);
            updateAirwayInfoForm.setVisible(true);
        });
    }
}